package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Employee employee, Long[] ids) {
        //先保存员工再处理中间表
        employeeMapper.insert(employee);
        //处理中间表  把数据插入中间表
        if (ids!=null&&ids.length>0){
            for (Long rid:ids){
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    //这里传入的ids是input页面的 select框中的数据 用于回显到右边
    //<select multiple class="form-control selfRoles" size="15" name="ids">
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除关系
        employeeMapper.deleteRelation(employee.getId());
        //关联关系
        if (ids!=null&&ids.length>0){
            for (Long rid:ids){
                employeeMapper.insertRelation(employee.getId(),rid);
            }
        }
    }
    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<Employee>(employees);
    }

    @Override
    public List<Role> queryRoleNameByEmpId(Long id) {
        List<Role> roles = employeeMapper.selectRoleNameByEmpId(id);
        return roles;
    }
}

